package com.zgp.embedding;

import cn.hutool.json.JSONUtil;
import com.zgp.domain.pojo.Rule;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.JsonReader;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 规则向量化处理组件
 * @author zgp
 * @version V1.0
 * @date 2025-04-20 20:33
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RuleEmbedding {
    private final VectorStore vectorStore;

    @PostConstruct
    public void init() {
        // 1. 模拟从数据源读取规则数据
        List<Rule> rules = List.of(
                new Rule("rule_01", "合约期", "合约期未满12个月", "提前更换套餐需支付剩余费用的10%作为违约金。"),
                new Rule("rule_02", "携号转网", "申请携号转网", "需满足：1. 无欠费 2. 当前套餐已到期 3. 非吉祥号段（如138/139开头）。")
        );
        // // 2. JSON数据是结构化的数据，不需要进行切割
        JsonReader jsonReader = new JsonReader(new ByteArrayResource(JSONUtil.toJsonStr(rules).getBytes()));
        List<Document> documents = jsonReader.get();
        // // 2.1 设置切片参数
        // TextSplitter textSplitter = new TokenTextSplitter(200, 100, 5, 10000, false);
        // List<Document> splitDocuments = textSplitter.apply(jsonReader.get());
        //

        // 3. 添加到向量数据库中
        vectorStore.add(documents);
    }
}
